package com.lsh.payment.core.strategy.query;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.payment.core.constant.BusinessConstant;
import com.lsh.payment.core.constant.RedisKeyConstant;
import com.lsh.payment.core.dao.redis.RedisStringDao;
import com.lsh.payment.core.model.payenum.cmpay.CmBizOrderStatus;
import com.lsh.payment.core.model.payenum.cmpay.CmBizResultCode;
import com.lsh.payment.core.model.payenum.cmpay.CmResultCode;
import com.lsh.payment.core.strategy.config.CmPayConfig;
import com.lsh.payment.core.strategy.vo.cmpay.CmQueryResponse;
import com.lsh.payment.core.util.pay.cm.HttpCmProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Project Name: lsh-payment
 * Created by peter on 16/11/6.
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Component
public class CmQueryStrategy implements QueryStrategy<CmQueryResponse> {

    private Logger logger = LoggerFactory.getLogger(CmQueryStrategy.class);

//    @Autowired
//    private RedisBaseService<String> redisBaseService;

    @Autowired
    private RedisStringDao redisStringDao;

    @Override
    public int getStrategyNo() {
        return BusinessConstant.CMQUERY;
    }

    /**
     * 查询第三方支付状态
     *
     * @param paramMap 查询参数
     * @return
     */
    @Override
    public CmQueryResponse queryPayStatusByParams(Map<String, Object> paramMap) {

        logger.info("收钱吧查询:" + JSON.toJSONString(paramMap));

        String data = null;
        try {

            org.json.JSONObject paramOrder = new org.json.JSONObject();

            paramOrder.put("terminal_sn", redisStringDao.get(RedisKeyConstant.PAY_CM_SN));
            paramOrder.put("client_sn", paramMap.get("payPaymentNo"));

            HttpCmProxy proxy = new HttpCmProxy(CmPayConfig.PAY_BASE);

            String result = proxy.query(paramOrder, redisStringDao.get(RedisKeyConstant.PAY_CM_SN), redisStringDao.get(RedisKeyConstant.PAY_CM_KEY));

            logger.info("result is " + result);

            JSONObject resultJson = JSON.parseObject(result);

            Integer outResultCode = resultJson.getInteger("result_code");

            if (outResultCode != CmResultCode.C200.getCode()) {

                resultJson.getString("error_code");
                resultJson.getString("error_message");

                return this.buildRsp(data);
            }

            JSONObject bizResponse = resultJson.getJSONObject("biz_response");

            String bizResultCode = bizResponse.getString("result_code");

            if (!bizResultCode.equals(CmBizResultCode.SUCCESS.getCode())) {
                bizResponse.getString("error_code");
                bizResponse.getString("error_message");

                return this.buildRsp(data);
            }

            data = bizResponse.getString("data");

        } catch (Exception e) {
            logger.error("", e);
        }

        CmQueryResponse cmQueryResponse = this.buildRsp(data);

        logger.info("请求收钱吧支付,第三方支付流水 返回:" + JSON.toJSONString(cmQueryResponse));

        return cmQueryResponse;
    }

    /**
     * @param resData
     * @return
     */
    private CmQueryResponse buildRsp(String resData) {
        CmQueryResponse cmQueryResponse = new CmQueryResponse();
        if (null == resData) {
            cmQueryResponse.setCode(BusinessConstant.PAY_REQUEST_FAIL);
            cmQueryResponse.setMsg("收钱吧支付通信失败");

            return cmQueryResponse;

        }
        cmQueryResponse = JSON.parseObject(resData, CmQueryResponse.class);

        if (null == cmQueryResponse) {
            cmQueryResponse.setCode(BusinessConstant.PAY_REQUEST_FAIL);
            cmQueryResponse.setMsg("收钱吧支付通信失败");

            return cmQueryResponse;
        }

        //校验返回参数
        if (cmQueryResponse.getOrder_status().equals(CmBizOrderStatus.PAID.getCode())) {

            cmQueryResponse.setCode(BusinessConstant.PAY_REQUEST_SUCCESS);
            cmQueryResponse.setMsg("收钱吧通信成功");
        } else {
            cmQueryResponse.setCode(BusinessConstant.PAY_REQUEST_FAIL);
            cmQueryResponse.setMsg("收钱吧支付通信失败");
            logger.info("收钱吧返回查询数据有误");
        }

        return cmQueryResponse;
    }

}
